/*******************************************************************************
 * Live Business Logic                          http://www.codeplex.com/livehome
 *                                              Microsoft Public License (Ms-PL)
 * Authors
 *  - Michael Flanakin  2008  http://www.michaelflanakin.com
 *
 * gadget
 *  - pages    : page constants
 *  - host     : host properties/functions
 *  - init     : initialization event
 *  - getImage : gets the current image from Live.com
 ******************************************************************************/

var live =
{
	/***************************************************************************
	* live.pages : standard page names
	**************************************************************************/
	pages:
	{
		main: 'main.htm',
		flyout: 'flyout.htm'
	},


	/***************************************************************************
	* live.properties : standard properties
	**************************************************************************/
	properties:
	{
		image: "image",
		setImage: function(val) { gadget.properties.set(live.properties.image, val); return live.image = val; },
		sync: function() { live.image = gadget.properties.get(live.properties.image); }
	},


	//***| properties |*********************************************************

	image: null,
	project: "livehome",
	dockedSize: { height: 68, width: 130 },
	undockedSize: { height: 256, width: 483 },
	updateCheckInterval: 24 * 60 * 60 * 1000,  // 24 hours
	refreshInterval: 60 * 1000,  // 60 seconds


	//***| live.getImage() |****************************************************

	getImage: function() {
		var http = util.http.create();
		http.open("get", "http://www.live.com", false);
		http.send();
		var html = http.responseText;
		var url = new String(html.match(/var g_img={url:'[\\\/A-Za-z0-9_\.\-]+'/gm));
		url = url.replace("\\", "/").replace("//", "/");
		if (url) { url = url.substring(16, url.length - 1); }
		return live.properties.setImage("http://live.com" + url);
	},


	//***| live.isUpToDate() |**************************************************

	isUpdateAvailable: function() {
		return util.version.isNewer(gadget.version, util.codeplex.getDefaultVersion(live.project));
	},


	//***| live.init() |********************************************************

	init: function() {
		live.properties.sync();
		page.init();
	}
};
util.addHandler(window, "onload", live.init);
